#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/centos7-get-server-info.sh | /bin/bash
#
#
############################################################################

set -e  # 遇到错误立即退出

# 生成简洁可读的服务器配置总结
OUTFILE="./server_info_summary_$(date +%F_%H%M%S).txt"

# 检查必要命令是否存在
check_command() {
    if ! command -v "$1" &> /dev/null; then
        echo "错误: 命令 '$1' 不存在，请先安装"
        exit 1
    fi
}

# 检查必需命令
for cmd in hostname uname free lsblk ip; do
    check_command "$cmd"
done

# 系统信息
HOSTNAME=$(hostname)
KERNEL=$(uname -r)
ARCH=$(uname -m)

# 操作系统检测（兼容多种Linux发行版）
if [ -f /etc/redhat-release ]; then
    OS=$(cat /etc/redhat-release)
elif [ -f /etc/os-release ]; then
    OS=$(grep PRETTY_NAME /etc/os-release | cut -d'=' -f2 | tr -d '"')
else
    OS="未知操作系统"
fi

# CPU信息（兼容没有lscpu的系统）
if command -v lscpu &> /dev/null; then
    CPU_MODEL=$(lscpu | grep "Model name" | awk -F: '{print $2}' | xargs)
    PHYSICAL_CPU=$(lscpu | grep "Socket(s)" | awk -F: '{print $2}' | xargs)
    CORES_PER_SOCKET=$(lscpu | grep "Core(s) per socket" | awk -F: '{print $2}' | xargs)
    TOTAL_THREADS=$(lscpu | grep "^CPU(s):" | awk -F: '{print $2}' | xargs)
else
    CPU_MODEL="未知（需要安装lscpu）"
    PHYSICAL_CPU="未知"
    CORES_PER_SOCKET="未知"
    TOTAL_THREADS=$(nproc 2>/dev/null || grep -c ^processor /proc/cpuinfo)
fi

# 内存信息
MEM_TOTAL=$(free -h | awk '/Mem:/ {print $2}')

# 硬件详细信息（需要root权限）
if [ "$(id -u)" -eq 0 ] && command -v dmidecode &> /dev/null; then
    MEM_MODULES=$(dmidecode -t memory 2>/dev/null | grep "Size:" | grep -v "No Module Installed" | sort | uniq -c | sed 's/^ *//g')
    MEM_EMPTY=$(dmidecode -t memory 2>/dev/null | grep -c "No Module Installed")
    MB_MANU=$(dmidecode -t baseboard 2>/dev/null | grep "Manufacturer" | awk -F: '{print $2}' | xargs)
    MB_MODEL=$(dmidecode -t baseboard 2>/dev/null | grep "Product Name" | awk -F: '{print $2}' | xargs)
    MB_SERIAL=$(dmidecode -t baseboard 2>/dev/null | grep "Serial Number" | awk -F: '{print $2}' | xargs)
else
    MEM_MODULES="需要root权限查看"
    MEM_EMPTY="需要root权限查看"
    MB_MANU="需要root权限查看"
    MB_MODEL="需要root权限查看"
    MB_SERIAL="需要root权限查看"
fi

# RAID和磁盘信息
if command -v lspci &> /dev/null; then
    RAID_INFO=$(lspci | grep -i raid | head -n1 | awk -F: '{print $3}' | xargs)
else
    RAID_INFO="未知（需要安装pciutils）"
fi

DISKS=$(lsblk -d -o NAME,SIZE 2>/dev/null | grep -v NAME | awk '{print $1 " (" $2 ")"}' | xargs -n1 echo "-")

# SMART状态检查（需要smartmontools）
if command -v smartctl &> /dev/null; then
    SMART_STATUS=$(for disk in $(lsblk -nd -o NAME,TYPE 2>/dev/null | awk '$2=="disk"{print $1}'); do 
        smartctl -H /dev/$disk 2>/dev/null | grep "SMART Health Status" | awk '{print $4}' | grep -q "OK" || echo "$disk FAIL"; 
    done)
else
    SMART_STATUS="需要安装smartmontools"
fi

# 网络信息
MAIN_IP=$(ip -4 addr show 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v "127.0" | head -n1)

if [ -n "$MAIN_IP" ]; then
    MAIN_IFACE=$(ip -o -4 addr show 2>/dev/null | grep "$MAIN_IP" | awk '{print $2}')
    if [ -n "$MAIN_IFACE" ]; then
        if command -v ethtool &> /dev/null; then
            IFACE_SPEED=$(ethtool $MAIN_IFACE 2>/dev/null | grep "Speed" | awk '{print $2}')
        else
            IFACE_SPEED="未知（需要安装ethtool）"
        fi
        IFACE_MAC=$(cat /sys/class/net/$MAIN_IFACE/address 2>/dev/null)
    else
        MAIN_IFACE="未知"
        IFACE_SPEED="未知"
        IFACE_MAC="未知"
    fi
else
    MAIN_IP="未找到业务IP"
    MAIN_IFACE="未知"
    IFACE_SPEED="未知"
    IFACE_MAC="未知"
fi

# 获取系统负载和运行时间
LOAD_AVG=$(cat /proc/loadavg 2>/dev/null | awk '{print $1, $2, $3}')
UPTIME=$(uptime 2>/dev/null | sed 's/^.*up //' | sed 's/,.*$//')

# 获取磁盘使用情况
DISK_USAGE=$(df -h / 2>/dev/null | awk 'NR==2 {print $5}')
TOTAL_DISK=$(df -h / 2>/dev/null | awk 'NR==2 {print $2}')

{
echo "【系统信息】"
echo "主机名：$HOSTNAME"
echo "操作系统：$OS"
echo "内核版本：$KERNEL"
echo "架构：$ARCH"
echo "运行时间：$UPTIME"
echo "系统负载：$LOAD_AVG"
echo

echo "【CPU】"
echo "型号：$CPU_MODEL"
echo "物理CPU数量：$PHYSICAL_CPU"
echo "每CPU核心：$CORES_PER_SOCKET"
echo "总线程数：$TOTAL_THREADS"
echo

echo "【内存】"
echo "总容量：$MEM_TOTAL"
echo "内存条："
echo "$MEM_MODULES"
echo "空插槽：$MEM_EMPTY"
echo

echo "【主板】"
echo "厂商：$MB_MANU"
echo "型号：$MB_MODEL"
echo "序列号：$MB_SERIAL"
echo

echo "【硬盘】"
echo "根分区使用率：$DISK_USAGE (总容量: $TOTAL_DISK)"
echo "RAID 控制器：$RAID_INFO"
echo "磁盘列表："
echo "$DISKS"
if [ -z "$SMART_STATUS" ]; then
    echo "SMART 健康：全部 OK"
else
    echo "SMART 健康：部分磁盘异常 => $SMART_STATUS"
fi
echo

echo "【网络】"
echo "业务IP：$MAIN_IP"
echo "主网卡：$MAIN_IFACE @ $IFACE_SPEED"
echo "MAC地址：$IFACE_MAC"
echo

echo "=== 脚本执行信息 ==="
echo "生成时间：$(date)"
echo "执行用户：$(whoami)"
if [ "$(id -u)" -eq 0 ]; then
    echo "权限：root用户"
else
    echo "权限：普通用户（部分信息需要root权限）"
fi
echo "输出文件：$OUTFILE"
} > "$OUTFILE"

echo "总结版配置信息已保存到: $OUTFILE"